﻿using System.Globalization;
using System.Resources;
using YWM.Common.Extensions;

namespace YWM.Common
{
    public static class LangUtil
    {
        /// <summary>
        /// 根据name获取对应的语言
        /// </summary>
        /// <param name="name"></param>
        /// <param name="resourceManager"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public static string GetString(string name, ResourceManager resourceManager, string culture)
        {
            string value = name;
            if (resourceManager != null)
            {
                try
                {
                    CultureInfo ci = new CultureInfo(culture);
                    string temp = resourceManager.GetString(name, ci);
                    if (!temp.IsNullOrWhiteSpace())
                    {
                        value = temp;
                    }
                }
                catch { }
            }
            return value;
        }

        /// <summary>
        /// 设置语言
        /// </summary>
        /// <param name="culture"></param>
        public static void SetLang(string culture)
        {
            if (culture == CultureInfo.CurrentUICulture.Name)
            {
                return;
            }
            CultureInfo newCulture = new CultureInfo(culture);
            CultureInfo.CurrentCulture = newCulture;
            CultureInfo.CurrentUICulture = newCulture;
        }
    }

}
